﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorldOnFire
{
    public class CShield : ABCObject
    {
        public enum eShieldState
        { 
            ACTIVE, 
            INACTIVE, 
            CHARGING, 
            COUNT 
        };

        private eShieldState[] m_eShieldState;
        private float[] m_fShieldStrength;
        private float[] m_fShieldMaximumStrength;
        private float[] m_fShieldRechargeTime;
        private float[] m_fShieldInactiveTime;
        private float[] m_fStateTimer;

        public void SetShieldMaximumStrength(int _nIndex, float _fStrength)
        { m_fShieldMaximumStrength[_nIndex] = _fStrength; }

        public void SetShieldInactiveTime(int _nIndex, float _fTime)
        { m_fShieldRechargeTime[_nIndex] = _fTime; }

        public void SetShieldRechargeTime(int _nIndex, float _fTime)
        { m_fShieldRechargeTime[_nIndex] = _fTime; }

        public void SetShieldState(int _nIndex, eShieldState _eShieldState)
        {
            // Reset the state timer
            m_fStateTimer[_nIndex] = 0.0f;

            // If the shield is being set to active
            if( _eShieldState == eShieldState.ACTIVE )
            {
                // Reset the shield's strength
                m_fShieldStrength[_nIndex] = m_fShieldMaximumStrength[_nIndex];
            }
            
            // Change the shield state
            this.ChangeState(_nIndex, _eShieldState);
        }

        public override void Initialize(int _nObjectCount)
        {
            // Initialize the base members
            base.Initialize(_nObjectCount);

            // Initialize the arrays
            m_eShieldState = new eShieldState[_nObjectCount];
            m_fShieldStrength = new float[_nObjectCount];
            m_fShieldMaximumStrength = new float[_nObjectCount];
            m_fShieldRechargeTime = new float[_nObjectCount];
            m_fShieldInactiveTime = new float[_nObjectCount];
            m_fStateTimer = new float[_nObjectCount];
        }

        public override void Update(float _fDeltaTime)
        {
            // Loop through each shield
            for (int iShield = 0; iShield < m_nObjectCount; ++iShield)
            {
                // Increment the state timer
                m_fStateTimer[iShield] += _fDeltaTime;

                // Switch based on shield state
                switch ((int)m_eShieldState[iShield])
                {
                    // If the shield is active
                    case (int)eShieldState.ACTIVE:
                        {
                            // If the shield has been broken
                            if (m_fShieldStrength[iShield] < 0.0f)
                            {
                                // Change the state to inactive
                                this.ChangeState(iShield, eShieldState.INACTIVE);
                            }
                        }
                        break;
                    // If the shield is inactive
                    case (int)eShieldState.INACTIVE:
                        {
                            // If the shield has been inactive long enough
                            if (m_fStateTimer[iShield] > m_fShieldInactiveTime[iShield])
                            {
                                // Change the state to charging
                                this.ChangeState(iShield, eShieldState.CHARGING);
                            }
                        }
                        break;
                    // If the shield is charging
                    case (int)eShieldState.CHARGING:
                        {
                            // If the shield has been charging long enough
                            if (m_fStateTimer[iShield] > m_fShieldRechargeTime[iShield])
                            {
                                // Reset the shield's strength
                                m_fShieldStrength[iShield] = m_fShieldMaximumStrength[iShield];

                                // Change the state to active
                                this.ChangeState(iShield, eShieldState.ACTIVE);
                            }
                        }
                        break;
                }
            }

            // Update the base members
            base.Update(_fDeltaTime);
        }

        private void ChangeState(int _nIndex, eShieldState _eNewState)
        {
            // Set the new shield state
            m_eShieldState[_nIndex] = _eNewState;

            // Reset the state timer
            m_fStateTimer[_nIndex] = 0.0f;
        }

    }
}
